package com.datasecops.scannerservices.timer;

import com.datasecops.scannerservices.service.HeartbeatServer;
import com.datasecops.scannerservices.service.RegServer;
import com.datasecops.scannerservices.util.ByteStreamHelper;
import com.datasecops.scannerservices.util.HttpURLHelper;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 心跳
 *
 * @author: SunLingzhi
 * @time: 2022/8/8 14:34
 */
@Configuration
@EnableScheduling
@Service
@Slf4j
public class HeartbeatTask {

    //本机IP
    @Value("${server.address}")
    private String ip;

    //本机Port
    @Value("${server.port}")
    private Integer port;

    //宿主IP
    @Value("${host.address}")
    private String hostIp;

    //宿主Port
    @Value("${host.port}")
    private Integer hostPort;

    //心跳地址
    private String heartbeatAddress = null;

    @Resource
    private HeartbeatServer heartbeatServer;


    /**
     * 发送心跳数据，每30s
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/8 14:36
     */
    @Scheduled(cron = "0/30 * * * * ?")
    private void sendHeartbeat() {
        if (null == heartbeatAddress) {
            heartbeatAddress = "http://" + hostIp + ":" + hostPort + "/EE/DlpSrvInfoToClient?Type=DlpScannerHeartbeat";
        }
        beganHeartbeat();
    }
     /**
      *发送停止信息
      * @Description
      * @author GeXiaoLong
      * @time  17:03
      * @param
      * @return
      */
 /*   @Scheduled(cron = "0/45 * * * * ?")
    private void sendStopMessage(){
        if (null == heartbeatAddress) {
            heartbeatAddress = "http://" + hostIp + ":" + hostPort + "/EE/DlpSrvInfoToClient?Type=DlpScannerHeartbeat";
        }
        beganHeartbeat();
    }*/

    /**
     * 发送心跳数据
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/10 9:23
     */
    private void beganHeartbeat() {
        byte[] paramData = getParamData();
        if (null == paramData || paramData.length <= 0) {
            log.error("Failed method beganRegister paramData is empty hostIp:" + hostIp + " &hostPort:" + hostPort);
            return;
        }
        String resultData = HttpURLHelper.connection(heartbeatAddress,paramData);
        if (null == resultData){
            log.error("Failed Method beganHeartbeat resultData is empty");
            return;
        }
        heartbeatServer.disposeResultData(resultData);
    }


    /**
     * 获取参数
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/10 9:49
     */
    private static byte[] getParamData() {
        Element rootNode = DocumentHelper.createElement("Parameters");

        Element idNode = DocumentHelper.createElement("Parameter");
        idNode.addAttribute("name", "ServerId");
        idNode.addAttribute("value", RegServer.serverId);
        rootNode.add(idNode);

        Element statusNode = DocumentHelper.createElement("Parameter");
        statusNode.addAttribute("name", "Status");
        statusNode.addAttribute("value", "1");
        rootNode.add(statusNode);

        byte[] paramData = null;
        try {
            String strParamXml = ByteStreamHelper.toXmlString(rootNode);
            paramData = ByteStreamHelper.formatParamDataFlow(strParamXml);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return paramData;
    }
}
